unit fServerConfig;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, SpTBXControls, StdCtrls, SpTBXEditors, ExtCtrls, OmniXml, OmniXmlConf;

type
  TfmServerConfig = class(TForm)
    lblDBServer: TSpTBXLabel;
    edtDBServer: TSpTBXEdit;
    edtDBName: TSpTBXEdit;
    lblDBName: TSpTBXLabel;
    edtDBUserName: TSpTBXEdit;
    lblDBUser: TSpTBXLabel;
    edtDBPassword: TSpTBXEdit;
    lblDBPassword: TSpTBXLabel;
    Bevel1: TBevel;
    btnTest: TSpTBXButton;
    btnOk: TSpTBXButton;
    btnCancel: TSpTBXButton;
    procedure btnTestClick(Sender: TObject);
    procedure btnOkClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
    FConfFile: TXmlConf;
    procedure SaveConfig;
    procedure LoadConfig;
  public
    { Public declarations }
  property
    ConfFile: TXmlConf read FConfFile;
  end;

var
  fmServerConfig: TfmServerConfig;
  cServerConfigFile: WideString = 'config.xml';
implementation


uses
  fServerDataModule, uDAInterfaces;

{$R *.dfm}

procedure TfmServerConfig.btnOkClick(Sender: TObject);
var
  str: WideString;
  conn: IDAConnection;
  drv: IDADriver;
begin
  str := WideFormat('ADO?AuxDriver=SQLOLEDB.1;Server=%s;Database=%s;UserID=%s;Password=%s',
                    [edtDBServer.Text,edtDBName.Text,edtDBUserName.Text,edtDBPassword.Text]);
  with ServerDataModule do
  begin
    drv := DriverManager.DriverByDriverID('ADO');
    conn := drv.NewConnection();
//    conn := ConnectionManager.NewConnection('SysConnection', False);
    conn.ConnectionString := str;
    try
      conn.Open();
    finally
      if (Conn <> nil) and conn.Connected then begin
        ConnectionManager.Connections.ConnectionByName('SysConnection').ConnectionString := str;
        SaveConfig;
        ModalResult := mrOk;
      end
      else
      begin
        ShowMessage('Connection failed');
      end;
     end;
  end;
end;

procedure TfmServerConfig.btnTestClick(Sender: TObject);
var
  str: WideString;
  conn: IDAConnection;
  drv: IDADriver;
begin
  str := WideFormat('ADO?AuxDriver=SQLOLEDB.1;Server=%s;Database=%s;UserID=%s;Password=%s',
                    [edtDBServer.Text,edtDBName.Text,edtDBUserName.Text,edtDBPassword.Text]);
  with ServerDataModule do
  begin
    drv := DriverManager.DriverByDriverID('ADO');
    conn := drv.NewConnection();

//    conn := ConnectionManager.NewConnection('SysConnection', False);
    conn.ConnectionString := str;
    try
      conn.Open();
    finally
      if (Conn <> nil) and conn.Connected then begin
        ShowMessage('Connection successful');
      end
      else
      begin
        ShowMessage('Connection failed');
      end;
     end;
  end;
end;

procedure TfmServerConfig.FormCreate(Sender: TObject);
begin
  FConfFile := TXmlConf.Create(cApplicationPath + cServerConfigFile);
  LoadConfig;
end;

procedure TfmServerConfig.FormDestroy(Sender: TObject);
begin
  FConfFile.Free;
end;

procedure TfmServerConfig.LoadConfig;
begin
  edtDBServer.Text := FConfFile.ReadWideString('system','dbserver','localhost');
  edtDBName.Text := FConfFile.ReadWideString('system','dbname','master');
  edtDBUserName.Text := FConfFile.ReadWideString('system','dbuser','sa');
  edtDBPassword.Text := FConfFile.ReadWideString('system','dbpassword','');
  ServerDataModule.ConnectionManager.Connections.ConnectionByName('SysConnection').ConnectionString :=
    FConfFile.ReadString('system','dbconnectionstring','');
  cADOConnectionString := 'Provider=SQLOLEDB.1;Persist Security Info=False;' +
                          'User ID=' + edtDBUserName.Text + ';'+
                          'Password=' + edtDBPassword.Text + ';'+
                          'Initial Catalog=' + edtDBName.Text + ';'+
                          'Data Source=' + edtDBServer.Text;           
end;

procedure TfmServerConfig.SaveConfig;
begin
  FConfFile.WriteWideString('system','dbserver',edtDBServer.Text);
  FConfFile.WriteWideString('system','dbname',edtDBName.Text);
  FConfFile.WriteWideString('system','dbuser',edtDBUserName.Text);
  FConfFile.WriteWideString('system','dbpassword',edtDBPassword.Text);
  FConfFile.WriteString('system','dbconnectionstring',
                         ServerDataModule.ConnectionManager.Connections.ConnectionByName('SysConnection').ConnectionString);
  cADOConnectionString := 'Provider=SQLOLEDB.1;Persist Security Info=False;' +
                          'User ID=' + edtDBUserName.Text + ';'+
                          'Password=' + edtDBPassword.Text + ';'+
                          'Initial Catalog=' + edtDBName.Text + ';'+
                          'Data Source=' + edtDBServer.Text;                         
//  FConfFile.WriteString('system','adoconnectionstring', cADOConnectionString);
end;

end.
